<template>
  <div>
    <el-form
      ref="form"
      :model="form"
      label-width="80px">
      <el-form-item
        :rules="[{ required: true, message: '账号不能为空'}]"
        label="账号"
        prop="name">
        <el-input v-model="form.name"/>
      </el-form-item>
      <el-form-item
        label="密码">
        <el-input v-model="form.pass"/>
      </el-form-item>
      <el-form-item label="头像">
        <w-image-up
          ref="UP_IMAGE"
          :souce-url="form.icon"/>
      </el-form-item>
      <el-form-item>
        <el-button
          :disabled="!form.name"
          type="primary"
          size="mini"
          @click="onSubmit">确定</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import WImageUp from '@/components/global/imageUp/oss'
import { mapGetters } from 'vuex'
export default {
  name: 'AdminAccountAdministrator',
  components: {
    WImageUp
  },
  data() {
    return {
      form: {
        name: '',
        icon: '',
        pass: ''
      }
    }
  },
  computed: {
    ...mapGetters(['user'])
  },
  mounted() {
    this.form.name = this.user.name
    this.form.icon = this.user.icon
  },
  methods: {
    // 提交
    async onSubmit() {
      try {
        this.form.icon = await this.$refs['UP_IMAGE'].upImage()
        await this.$store.dispatch('MODIFY_USER_DATA', this.form)
        this.$w_popview.msgSuccess('修改成功，请重新登录', {
          onClose: () => {
            this.$store.dispatch('LOG_OUT').then(() => {
              this.$router.push('/')
            })
          }
        })
      } catch (e) {
        this.$w_popview.msgError(e.msg)
      }
    }
  }
}
</script>

<style lang="scss" scoped>

  .icon {
    width: 200px;
  }

</style>
